<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check = "false"
    >
      <ArticleItem
        v-for="obj in list"
        :key="obj.art_id"
        :obj="obj"
        :isShow="true"
      ></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '@/views/Home/components/ArticleItem'
import { getSearchResultAPI } from '@/api'
export default {
  name: 'SearchResult',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 2
    }
  },
  components: {
    ArticleItem
  },
  async created () {
    const res = await getSearchResultAPI({ q: this.$route.params.keywords })
    this.list = res.data.data.results
  },
  methods: {
    async onLoad () {
      const res = await getSearchResultAPI({ q: this.$route.params.keywords, page: this.page })
      if (res.data.data.results.length === 0) {
        this.finished = true
      } else {
        this.list = [...this.list, ...res.data.data.results]
        this.page = this.page + 1
        this.loading = false
      }
    }

  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
